﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MyPublish.Connection
{
    public class DataConnectionFactory
    {
        public static DataConnectionFactory Instance { get; private set; }
        
        static DataConnectionFactory()
        {
            Instance = new DataConnectionFactory();
            LoadAssembly("MyPublish.Connection.Syndication");
        }

        private static void LoadAssembly(string name)
        {
            Assembly fopCompositonAssembly = Assembly.Load(name);
            Type[] types = fopCompositonAssembly.GetTypes();
            foreach (Type type in types)
            {
                object[] attributes = type.GetCustomAttributes(typeof(DataConnectionProviderAttribute), true);
                foreach (DataConnectionProviderAttribute attribute in attributes)
                {
                    Instance.Providers.Add(attribute.Name.ToLower(),type);
                }
            }
        }

        private Dictionary<string, Type> Providers { get; set; }
        private DataConnectionFactory() 
        {
            Providers = new Dictionary<string, Type>();
        }

        public DataConnection CreateConnection()
        {
            throw new NotImplementedException();
        }

        public DataConnectionProvider CreateConnectionProvider(string name)
        {
            name = name.ToLower();
            return Activator.CreateInstance(Providers[name]) as DataConnectionProvider;
        }
    }
}
